package server;

import java.util.Timer;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import Util.Task;
import Util.TaskQueue;

public class StartServer {
	 // create thread pool
	public static ExecutorService fixedThreadPool = Executors.newFixedThreadPool(5);
    public static void main(String[] args) {
    	// create one thread to start Server
        new Thread(new Runnable() {
    			public void run() {
    				ChattingServer cs= new ChattingServer();
    				cs.startServer();	
    			}
    	   }).start();
        // create one thread to monitor the TaskQueue
        new Thread(new Runnable() {
    			public void run() {
    				while (true) {
    					while(!TaskQueue.queue.isEmpty()) {    						
    						doTask();	
    					}
    					try {
    						Thread.sleep(10);
    					} catch (InterruptedException e) {
    						// TODO Auto-generated catch block
    						e.printStackTrace();
    					}
    				}
    			}
    		}).start();
    
    
    }
    
    // do Task
    public static void doTask() {
		try {
			Task task = TaskQueue.outQueue();
			System.out.println(task.toString());
			fixedThreadPool.execute(task);

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
}